package com.tedu.egspringbootmvc.common.exception;

import com.tedu.egspringbootmvc.common.result.JsonResult;
import com.tedu.egspringbootmvc.common.result.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandlerMethodArgumentNotValidException (MethodArgumentNotValidException e){
        String message = e.getFieldError().getDefaultMessage();
        log.error(message,e);
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }
}
